<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input
          v-model="queryParams.age"
          placeholder="请输入年龄"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人名称" prop="createByName">
        <el-input
          v-model="queryParams.createByName"
          placeholder="请输入创建人名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新人名称" prop="updateByName">
        <el-input
          v-model="queryParams.updateByName"
          placeholder="请输入更新人名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['admin:test3:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['admin:test3:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['admin:test3:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['admin:test3:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading"
              element-loading-text="拼命加载中"
              element-loading-spinner="el-icon-loading"
              :data="test3List" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="name" />

      <el-table-column label="年龄" align="center" prop="age" />

  <el-table-column label="图片" align="center" prop="picture">
    <template slot-scope="scope">
      <el-image
          style="width: 100px; height: 48px"
          :src="scope.row.picture"
          fit="cover"
          :preview-src-list="previewpictureList"
          title="点击查看图片"
          @click="previewpicture(scope.row)"
      ></el-image>
    </template>
  </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" show-overflow-tooltip="true"/>
      <el-table-column label="创建人名称" align="center" prop="createByName" />

      <el-table-column label="修改时间" align="center" prop="updateTime" show-overflow-tooltip="true"/>
      <el-table-column label="更新人名称" align="center" prop="updateByName" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['admin:test3:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['admin:test3:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改测试3对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form class="form-wrapper" ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
  <el-form-item label="图片" prop="picture">
    <div style="display: flex;flex-direction: row;align-items: center">
      <el-upload
          action="/dev-api/common/upload/file"
          :on-success="handleSuccesspicture"
          list-type="picture-card"
          :on-error="handleErrorpicture"
          :limit="3"
          :file-list="defaultpictureList"
          :before-upload="beforeUploadpicture"
          style="margin-left: 10px"
      >
        <i class="el-icon-plus"></i>
      </el-upload>
    </div>

  </el-form-item>
        <el-form-item label="创建人名称" prop="createByName">
          <el-input v-model="form.createByName" placeholder="请输入创建人名称" />
        </el-form-item>
        <el-form-item label="更新人名称" prop="updateByName">
          <el-input v-model="form.updateByName" placeholder="请输入更新人名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" style="text-align: center">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTest3, getTest3, delTest3, addTest3, updateTest3, exportTest3 } from "@/api/admin/test3";

export default {
  name: "Test3",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 测试3表格数据
      test3List: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      defaultpictureList: [],
      previewpictureList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        age: undefined,
        createByName: undefined,
        updateByName: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询测试3列表 */
    getList() {
      this.loading = true;
      listTest3(this.queryParams).then(res => {
        if(res.code === 200){
          this.test3List = res.data.rows;
          this.total = Number(res.data.total);
          this.loading = false;
        }
      }).catch(()=>{
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        age: undefined,
        picture: undefined,
        createTime: undefined,
        createByName: undefined,
        updateTime: undefined,
        updateByName: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加测试3";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTest3(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改测试3";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateTest3(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addTest3(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除测试3编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTest3(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有测试3数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTest3(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
            handleSuccesspicture(response, file, fileList) {
              console.log('上传成功', response, file, fileList);
              if (response.code === 200) {
                this.form.picture = response.msg
              } else {
                this.$message.error('上传失败');
              }
            },
            // 点击图片，大图预览多张图片
            previewpicture (row) {
              var srclist = []
              if (row.picture) {
                srclist.push(row.picture)
                this.previewpictureList = srclist
              }
            },
            handleErrorpicture(err, file, fileList) {
              // 文件上传失败的回调函数
              console.log('上传失败', err, file, fileList);
            },
            beforeUploadpicture(file) {
              // 在文件上传之前的钩子函数，可以在这里进行文件格式、大小等的校验
              const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
              const isLt500KB = file.size / 1024 <= 5000;
              if (!isJPG) {
                this.$message.error('只能上传jpg/png文件!');
              }
              if (!isLt500KB) {
                this.$message.error('文件大小不能超过5M!');
              }
              return isJPG && isLt500KB;
            },
  }
};
</script>


<style rel="stylesheet/scss" lang="scss">
  .form-wrapper{
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
  }

  .form-wrapper .el-form-item{
    width: 50%;
  }
</style>



